import { createRouter, createWebHistory } from 'vue-router'
// 基础页
import IndexView from '@/views/index.vue'
// 数据大屏
import DataBoardView from '@/views/DataBoard/index.vue'
// 登录页
import LoginView from '@/views/login/index.vue'
// 密码找回页面
import FindView from '@/views/find/index.vue'
// 首页
import HomeView from '@/views/home/index.vue'
// 用户页
import UserView from '@/views/user/index.vue'
// 咨询预约页
import ConsultationView from '@/views/consultation/index.vue'
// 心理课程页
import CourseView from '@/views/course/index.vue'
// 课程详情页
import CourseInfoView from '@/views/course/CourseInfo/index.vue'
// 心理评测页
import EvaluationView from '@/views/evaluation/index.vue'
// 预警干预页
import InterventionView from '@/views/intervention/index.vue'
// 个人中心页
import PersonView from '@/views/person/index.vue'
// 权限演示页面
import PermissionDemoView from '@/views/permission-demo/index.vue'
// 第一个数据图详情页
import Graph1View from '@/views/home/graph1/index.vue'
// 第二个数据图详情页
import Graph2View from '@/views/home/graph2/index.vue'
// 第三个数据图详情页
import Graph3View from '@/views/home/graph3/index.vue'
// 第四个数据图详情页
import Graph4View from '@/views/home/graph4/index.vue'
// AI页面
import AIView from '@/views/ai/index.vue'
// AI助教
import AIAssistantView from '@/views/ai/assistant/index.vue'
// AI心理分析
import AIAnalyzeView from '@/views/ai/analyze/index.vue'
// 用户档案页面
import UserinfoView from '@/views/user/userinfo/index.vue'
// 基础资料
import BaseView from '@/views/user/userprofile/base/index.vue'
// 心理档案
import MentalView from '@/views/user/userprofile/mental/index.vue'
// 个性化支持方案
import SupportSolutionView from '@/views/user/userprofile/mental/SupportSolution/index.vue'
// AI心理咨询报告
import AICounselingReportView from '@/views/user/userprofile/mental/AICounselingReport/index.vue'
// AI心理自助使用情况
import AIMentalSelfHelpUseView from '@/views/user/userprofile/mental/AIMentalSelfHelpUse/index.vue'
// 线下咨询记录
import FaceCounselingRecordsView from '@/views/user/userprofile/mental/FaceCounselingRecords/index.vue'
// 线下干预记录
import FaceInterventionRecordsView from '@/views/user/userprofile/mental/FaceInterventionRecords/index.vue'
// 心理评测报告
import MentalAssessmentReportView from '@/views/user/userprofile/mental/MentalAssessmentReport/index.vue'
// 心理评测报告子页面
import MentalReportView from '@/views/user/userprofile/mental/MentalAssessmentReport/report/index.vue'
// 行为数据报告
import BehaviorView from '@/views/user/userprofile/behavior/index.vue'
// 组织结构页面
import StructView from '@/views/user/struct/index.vue'
// 针对某一个用户的档案页面
import UserProfileView from '@/views/user/userprofile/index.vue'
// 用户管理页面
import UserManagementView from '@/views/user/UserManagement/index.vue'
// 添加评测页面
import AddEvaView from '@/views/evaluation/add/index.vue'
// 报告记录页面
import ReportRecordView from '@/views/evaluation/reportrecord/index.vue'
// 团测进度页面
import GroupProgressView from '@/views/evaluation/groupprogress/index.vue'
// 团测进度完成情况页面
import GroupFinshInfoView from '@/views/evaluation/groupprogress/finshinfo/index.vue'
// 团测编辑计划页面
import GroupEditPlanView from '@/views/evaluation/groupprogress/editplan/index.vue'
// 团测编辑计划页面（下一页）
import GroupEditPlanNextView from '@/views/evaluation/groupprogress/editplan/next/index.vue'
// 团体报告页面
import GroupReportView from '@/views/evaluation/groupreport/index.vue'
// 新增概览
import WarningOverView from '@/views/intervention/over/index.vue'
// 新增预警
import AddWarningView from '@/views/intervention/addwarning/index.vue'
// 预警名单
import WarningListView from '@/views/intervention/list/index.vue'
// 预警评估
import WarningEvaView from '@/views/intervention/eva/index.vue'
// 预警干预
import WarningInterView from '@/views/intervention/warningInter/index.vue'
// 预警设置
import WarningSetView from '@/views/intervention/set/index.vue'

// 个人中心页面
import PersonCenterView from '@/views/person/center/index.vue'
// 权限页面
import PersonPermissionView from '@/views/person/permission/index.vue'

// 咨询师页面
import CounselorView from '@/views/consultation/counselor/index.vue'
// 预约记录页面
import AppointmentView from '@/views/consultation/appointment/index.vue'
// 咨询记录页面
import RecordView from '@/views/consultation/record/index.vue'
import {ElMessage} from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', name: 'DataBoard', component: DataBoardView },
    {
      // 首页相关页面
      path: '/index', name: 'index', component: IndexView, redirect: '/home', children: [
      { path: '/home', name: 'home', component: HomeView, },
      // AI助教相关页面
      {path: '/ai', name: 'ai', component: AIView, redirect: '/ai/assistant', children: [
        { path: '/ai/assistant', name: 'ai-assistant', component: AIAssistantView, },
        { path: '/ai/analyze', name: 'ai-analyze', component: AIAnalyzeView, },
      ]},
      // 用户相关页面
      { path: '/user', name: 'user', component: UserView, redirect: '/user/usermanagement', children: [
        { path: '/userinfo', name: 'userinfo', component: UserinfoView, },
        { path: '/struct', name: 'struct', component: StructView, },
        { path: '/profile', name: 'profile', component: UserProfileView, redirect: '/user/profile/base', children: [
            { path: '/user/profile/base', name: 'user-profile-base', component: BaseView, },
            { path: '/user/profile/mental', name: 'user-profile-mental', component: MentalView, },
            { path: '/user/profile/behavior', name: 'user-profile-behavior', component: BehaviorView, },
            { path: '/user/profile/AICounselingReport', name: 'AI-counseling-report', component: AICounselingReportView, },
            { path: '/user/profile/AIMentalSelfHelpUse', name: 'AI-mental-self-help-use', component: AIMentalSelfHelpUseView, },
            { path: '/user/profile/FaceCounselingRecords', name: 'face-counseling-records', component: FaceCounselingRecordsView, },
            { path: '/user/profile/FaceInterventionRecords', name: 'face-intervention-records', component: FaceInterventionRecordsView, },
            { path: '/user/profile/SupportSolution', name: 'support-solution', component: SupportSolutionView, },
            { path: '/user/profile/MentalAssessmentReport', name: 'mental-assessment-report', component: MentalAssessmentReportView, },
            { path: '/user/profile/mental/report', name: 'mental-report', component: MentalReportView, },
        ]},
        { path: '/user/usermanagement', name: 'user-usermanagement', component: UserManagementView, },
      ]},
      // 咨询预约相关页面
      { path: '/consultation', name: 'consultation', component: ConsultationView, redirect: '/counselor', children: [
        { path: '/counselor', name: 'counselor', component: CounselorView, },
        { path: '/appointment', name: 'appointment', component: AppointmentView, },
        { path: '/record', name: 'record', component: RecordView, },
      ]},
      // 心理课程相关页面
      { path: '/course', name: 'course', component: CourseView, },
      { path: '/course/info', name: 'course-info', component: CourseInfoView, },
      // 心理评测相关页面
      { path: '/evaluation', name: 'evaluation', component: EvaluationView, redirect: '/addeva', children: [
        { path: '/addeva', name: 'addeva', component: AddEvaView, },
        { path: '/reportrecord', name: 'reportrecord', component: ReportRecordView, },
        { path: '/groupprogress', name: 'groupprogress', component: GroupProgressView, },
        { path: '/groupreport', name: 'groupreport', component: GroupReportView, },
        { path: '/finshinfo', name: 'finshinfo', component: GroupFinshInfoView, },
        { path: '/evaluation/editplan', name: 'evaluation-editplan', component: GroupEditPlanView, },
        { path: '/evaluation/editplannext', name: 'evaluation-editplannext', component: GroupEditPlanNextView, },

      ]},
      // 预警干预相关页面
      { path: '/intervention', name: 'intervention', component: InterventionView, redirect: '/warningover', children: [
        { path: '/warningover', name: 'warningover', component: WarningOverView, },
        { path: '/addwarning', name: 'addwarning', component: AddWarningView, },
        { path: '/warninglist', name: 'warninglist', component: WarningListView, },
        { path: '/warningeva', name: 'warningeva', component: WarningEvaView, },
        { path: '/warninginter', name: 'warninginter', component: WarningInterView, },
        { path: '/warningset', name: 'warningset', component: WarningSetView, },

      ]},
      // 个人中心相关页面
      { path: '/person', name: 'person', component: PersonView, redirect: '/person/center', children: [
        { path: '/person/center', name: 'person-center', component: PersonCenterView, },
        { path: '/person/permission', name: 'person-permission', component: PersonPermissionView, },

      ]},
      // 权限演示页面
      { path: '/permission-demo', name: 'permission-demo', component: PermissionDemoView, },
      { path: '/graph1', name: 'graph1', component: Graph1View, },
      { path: '/graph2', name: 'graph2', component: Graph2View, },
      { path: '/graph3', name: 'graph3', component: Graph3View, },
      { path: '/graph4', name: 'graph4', component: Graph4View, },

    ]},
    // 登录
    { path: '/login', name: 'login', component: LoginView, },
    // 注册
    { path: '/find', name: 'find', component: FindView, },
  ],
})

router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/find']
  const token = localStorage.getItem('token')

  // 免登录白名单直接放行
  if (whiteList.includes(to.path)) {
    return next()
  }

  // 已登录放行，否则跳转登录并记录重定向目标
  if (token) {
    return next()
  } else {
      ElMessage.error('请先登录系统')
      return next({ path: '/login' })
  }
})
export default router
